package com.mayspeed.jg;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import com.mayspeed.jg.command.JGCommand;
import com.mayspeed.jg.listener.JGListener;
import com.mayspeed.jg.util.FileUtil;
import com.mayspeed.jg.util.JGUtil;

import net.milkbowl.vault.economy.Economy;

/**
 * 主类
 * @author May_Speed
 * @since 2017/4/22
 */
public class JingGong extends JavaPlugin{
	
	private static Economy economy = null;
	private static JingGong instance;
	File msg = new File(getDataFolder(), "message.yml");
	
	
	/**
	 * 初始化Vault经济
	 * @return
	 */
	private boolean initVault(){
        boolean hasNull = false;
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            if ((economy = economyProvider.getProvider()) == null) hasNull = true;
        }
        return !hasNull;
    }
	
	@Override
	public void onEnable() {
		ConsoleCommandSender ccs = Bukkit.getConsoleSender();
		if (!initVault()){
			ccs.sendMessage("§2精工 §e>> §fVault未成功初始化! 将会自动关闭插件");
			Bukkit.getPluginManager().disablePlugin(this);
		}
		Bukkit.getPluginManager().registerEvents(new JGListener(), this);
		Bukkit.getPluginCommand("jg").setExecutor(new JGCommand());
		if (!new File(getDataFolder(), "config.yml").exists()) {
			saveDefaultConfig();
		}
		if(!msg.exists()){
			saveResource("message.yml", true);
		}
		reloadConfig();
		instance = this;
	}
	
	public static JingGong getInstance(){ 
		return instance;
	}
	
	public FileConfiguration getMessage() {
		return FileUtil.loadYml(msg);
	}
	
	public Economy getEco() {
		return economy;
	}
	
//	/**
//	 * 对一个ItemStack进行精工
//	 * @return
//	 */
//	public ItemStack jingGong(Player player, int level) {
//		ItemStack is = JGUtil.getItemInMainHand(player);
//		ItemMeta im = is.getItemMeta();
//		List <String> lore = new ArrayList<String>();
//		//判断是否为空物品
//		if (is != null && is.getType() != Material.AIR) {
//			//判断是否含有Lore
//			if (!im.hasLore()) {
//				lore.add(JingGong.getInstance().getConfig().getString("JingGongItemTitle").replaceAll("&", "§"));
//				lore.add(" §6§l精工等级: §e★§r☆☆☆☆");
//				im.setLore(lore);
//				is.setItemMeta(im);
//				player.sendMessage(JingGong.getInstance().getMessage().getString("jgYes").replaceAll("&", "§"));
//				player.closeInventory();
//			}else {
//				lore.addAll(im.getLore());
//				if (lore.contains(JingGong.getInstance().getConfig().getString("JingGongItemTitle").replaceAll("&", "§"))) {
//					
//				}
//				for (int i = 0; i < lore.size(); i++) {
//					
//				}
//			}
//		}
//		return is;
//	}
	
	/**
	 * 对一个ItemStack进行精工
	 * @return
	 */
	public ItemStack jingGong(Player player) {
		ItemStack is = JGUtil.getItemInMainHand(player);
		ItemMeta im = is.getItemMeta();
		List <String> lore = new ArrayList<String>();
		//判断是否为空物品
		if (is != null && is.getType() != Material.AIR) {
			//判断是否含有Lore
			if (!im.hasLore()) {
				System.out.println("3");
				lore.add(JingGong.getInstance().getConfig().getString("JingGongItemTitle").replaceAll("&", "§"));
				lore.add(" §6§l精工等级: §e★§r☆☆☆☆");
				im.setLore(lore);
				is.setItemMeta(im);
				player.sendMessage(JingGong.getInstance().getMessage().getString("jgYes").replaceAll("&", "§"));
				JGUtil.takePlayerMoney(player, JingGong.getInstance().getConfig().getInt("Costs." + (JGUtil.getItemJingGongLevel(JGUtil.getItemInMainHand(player)))));
				player.closeInventory();
				return is;
			}else {
				lore.addAll(im.getLore());
				
				//遍历Lore
				for (int i = 0; i < lore.size(); i++) {
					//判断有无标题头
					if (lore.get(i).indexOf(JingGong.getInstance().getConfig().getString("JingGongItemTitle").replaceAll("&", "§")) != -1) {
						System.out.println("1-1");
						lore.set(i + 1," §6§l精工等级: " + JGUtil.getJingGongLevelToStarString((JGUtil.getItemJingGongLevel(is) + 1)));
						im.setLore(lore);
						is.setItemMeta(im);
						player.sendMessage(JingGong.getInstance().getMessage().getString("jgYes").replaceAll("&", "§"));
						JGUtil.takePlayerMoney(player, JingGong.getInstance().getConfig().getInt("Costs." + (JGUtil.getItemJingGongLevel(JGUtil.getItemInMainHand(player)))));			
						player.closeInventory();
						return is;
					}
				}
				
				//如果程序执行到了这里说明无标题头
				System.out.println("2-1");
				//是第一次精工
				lore.add(JingGong.getInstance().getConfig().getString("JingGongItemTitle").replaceAll("&", "§"));
				lore.add(" §6§l精工等级: §e★§r☆☆☆☆");
				im.setLore(lore);
				is.setItemMeta(im);
				player.sendMessage(JingGong.getInstance().getMessage().getString("jgYes").replaceAll("&", "§"));
				JGUtil.takePlayerMoney(player, JingGong.getInstance().getConfig().getInt("Costs." + (JGUtil.getItemJingGongLevel(JGUtil.getItemInMainHand(player)))));
				player.closeInventory();
				return is;
			}
		}
		return is;
	}
}
